package core;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;
import org.kxml2.io.KXmlSerializer;

public class Schedule {
	private Vector m_entries;

	public Schedule(){
		m_entries = new Vector();
	}
	
	public boolean isTimeBusy( DayOfWeek dayOfWeek, WeekDefinition week,  TimeOfTheClasses time){
		for (int i=0; i<m_entries.size(); i++){
			ScheduleEntry entry = (ScheduleEntry)m_entries.elementAt(i);
			if (entry.getDayOfWeek().equals(dayOfWeek)
				&& (entry.getWeek().equals(week)||entry.getWeek().equals(new WeekDefinition(2)))
				&& entry.getTime().equals(time)){
				return true;
			}
		}
		return false;
	}
	
	public boolean addEntry(ScheduleEntry entry){
		if (isTimeBusy( entry.getDayOfWeek(), entry.getWeek(), entry.getTime())){
			return false;
		}
		m_entries.addElement(entry);
		return true;
	}
	
	
	public void deleteEntry(ScheduleEntry entry){
		DayOfWeek dayOfWeek = entry.getDayOfWeek();
		WeekDefinition week = entry.getWeek();
		TimeOfTheClasses time = entry.getTime();
		for (int i=0; i<m_entries.size(); i++){
			ScheduleEntry m_entry = (ScheduleEntry)m_entries.elementAt(i);
			if (m_entry.getDayOfWeek().equals(dayOfWeek)
					&& m_entry.getWeek().equals(week)
					&& m_entry.getTime().equals(time)){
				m_entries.removeElementAt(i);
				break;
			}
		}
	}
	
	public void deleteEntry(DayOfWeek dayOfWeek, WeekDefinition week,  TimeOfTheClasses time){
		for (int i=0; i<m_entries.size(); i++){
			ScheduleEntry entry = (ScheduleEntry)m_entries.elementAt(i);
			if (entry.getDayOfWeek().equals(dayOfWeek)
					&& entry.getWeek().equals(week)
					&& entry.getTime().equals(time)){
				m_entries.removeElementAt(i);
				break;
			}
		}
	}
	
	public int entriesCount(){
		return m_entries.size();
	}
	
	public ScheduleEntry[] getEntriesForDay(DayOfWeek day, WeekDefinition week){
		Vector prepared = new Vector();
		Enumeration iter = m_entries.elements();
		while(iter.hasMoreElements()){
			ScheduleEntry entry = (ScheduleEntry)iter.nextElement();  
			if(entry.getDayOfWeek().equals(day) && (entry.getWeek().equals(week)||entry.getWeek().equals(new WeekDefinition(2)))){
				prepared.addElement(entry);
			}
		}
		ScheduleEntry ret[] = new ScheduleEntry[prepared.size()];
		for(int i = 0; i < ret.length; ++i){
			ret[i] = (ScheduleEntry)prepared.elementAt(i);
		}
		ScheduleEntry k = new ScheduleEntry(null, null, null, null);
		for (int i=(ret.length-1); i>0; i--)
			for (int j=0; j<i; j++)
				if (ret[j].getTime().getTimeNumber()>ret[j+1].getTime().getTimeNumber()){
					k=ret[j];
					ret[j]=ret[j+1];
					ret[j+1]=k;
				}
		return ret;
	}
	
	
	public void editEntry(DayOfWeek dayOfWeek, WeekDefinition week,  TimeOfTheClasses time, ScheduleEntry entry){
		for (int i=0; i<m_entries.size(); i++){
			ScheduleEntry m_entry = (ScheduleEntry)m_entries.elementAt(i);
			if (m_entry.getDayOfWeek().equals(dayOfWeek)
					&& m_entry.getWeek().equals(week)
					&& m_entry.getTime().equals(time)){
				m_entries.setElementAt(entry, i);
			}
		}
	}
	
	public ScheduleEntry getEntry(DayOfWeek dayOfWeek, WeekDefinition week,  TimeOfTheClasses time){
		for (int i=0; i<m_entries.size(); i++){
			ScheduleEntry entry = (ScheduleEntry)m_entries.elementAt(i);
			if (entry.getDayOfWeek().equals(dayOfWeek)
					&& entry.getWeek().equals(week)
					&& entry.getTime().equals(time)){
				return entry;
			}
		}
		return null;
	}
	
	public void deleteAll(){
		m_entries.removeAllElements();
	}
	
	public String toXml(){
		ByteArrayOutputStream byteArrOutStream = new ByteArrayOutputStream();
		OutputStreamWriter writer = new OutputStreamWriter(byteArrOutStream );
		
		KXmlSerializer serializer = new KXmlSerializer();
		try {
			serializer.startTag("", "Shedule");
			serializer.endTag(null, "Shedule");
			serializer.setOutput(writer);
			serializer.flush();
			byteArrOutStream.toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "";
	}
	
}